#!/bin/sh
PARAM1=$1
if [ "$PARAM1" = "" ]; then
  echo "$0 all|sipman|oldsipman|mrtg"
  exit
fi

runRsync ()
{
  SOURCE=$1
  DESTDIR=$2
  echo "Backup from $SOURCE to $DESTDIR."
  if [ ! -d $DESTDIR ]; then
    mkdir -p $DESTDIR
  else
    touch $DESTDIR
  fi
  rsync -avz -e ssh --delete --exclude=/dev --exclude=/mnt --exclude=/proc --exclude=/extra --exclude=/backup --exclude=/var/tmp --delete-excluded $SOURCE $DESTDIR
}

DOW=`date +%u`
case $DOW in
  (2|4)   BACKVER=di-do ;;
  (3|5)   BACKVER=wo-vr ;;
  (6)   BACKVER=za ;;
  (*)   echo "Skipping the backup today."
        exit ;;
esac
BACKDEVICE=/dev/cciss/c0d1p1
BACKDIR=/extra
echo "Mounting $BACKDEVICE on $BACKDIR."
mount -t ext2 $BACKDEVICE $BACKDIR
if [ -f "$BACKDIR/diskinfo-backup" ]; then
  if [ "$PARAM1" = "all" ] || [ "$PARAM1" = "sipman" ]; then
    runRsync / $BACKDIR/sipman01-$BACKVER | tee $BACKDIR/sipman01-`date -I`.log
    runRsync 10.1.1.112:/ $BACKDIR/sipman02-$BACKVER | tee $BACKDIR/sipman02-`date -I`.log
    runRsync 10.1.1.113:/ $BACKDIR/sipman03-$BACKVER | tee $BACKDIR/sipman03-`date -I`.log
  fi
  if [ "$PARAM1" = "all" ] || [ "$PARAM1" = "mrtg" ]; then
    runRsync 10.1.2.4:/ $BACKDIR/mrtg-$BACKVER | tee $BACKDIR/mrtg-`date -I`.log
  fi
  if [ "$PARAM1" = "all" ] || [ "$PARAM1" = "oldsipman" ]; then
    runRsync 10.1.1.15:/ $BACKDIR/sipman-$BACKVER | tee $BACKDIR/sipman-`date -I`.log
  fi
umount $BACKDIR
echo "Unmounting $BACKDIR."
else
  echo "Aborting rsync, disk not mounted."
fi

